package com.snts.synchronization.locator;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.io.UrlResource;

public class ServiceLocator {
	
	private URL contextConfigLocation;
	private ApplicationContext applicationContext;

	public ServiceLocator(URL contextConfigLocation) {
		super();
		this.contextConfigLocation = contextConfigLocation;
	}

	public Object getService(String name){
		if(applicationContext == null){
			applicationContext = new GenericXmlApplicationContext(new UrlResource(contextConfigLocation));
		}
		return applicationContext.getBean(name);
	}
	
	public static void main(String[] args) throws MalformedURLException {
		System.out.println(new URL("file://" + new File(".").getAbsolutePath() + "\\WebContent\\WEB-INF\\applicationContext.xml").toString());
		ServiceLocator serviceLocator = new ServiceLocator(new URL("file:///" + new File(".").getAbsolutePath() 
				                                           + "\\WebContent\\WEB-INF\\applicationContext.xml"));
		System.out.println(serviceLocator.getService("dataSource"));
		System.out.println(serviceLocator.getService("synchronization"));
		System.out.println(serviceLocator.getService("synchronization"));
		System.out.println(serviceLocator.getService("synchronization"));
		System.out.println(serviceLocator.getService("synchronization"));		
	}

}
